package com.code.base.acc.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.code.base.acc.persistence.AccPsnMapper;
import com.code.base.acc.pojo.AccPsn;
import com.code.base.info.pojo.InfoBld;
import com.code.base.info.pojo.InfoHouse;
import com.code.base.info.service.InfoBldService;
import com.code.base.info.service.InfoHouseService;
import com.code.module.process.service.ProcessService;
import com.code.util.MD5;
import com.code.util.PrimaryKeyUtil;


@Service
public class AccPsnService {

	@Resource
	private AccPsnMapper accPsnMapper;
	
	@Resource
	private InfoHouseService infoHouseService;
	
	@Resource
	private InfoBldService infoBldService;
	
	@Resource
	private ProcessService processService;
	
	public Long getInsertId() {
		return PrimaryKeyUtil.generatePrimaryKey();
	}

	public AccPsn getDataById(Long id) {
		return accPsnMapper.getDataById(id);
	}

	public List<AccPsn> getAllData() {
		return accPsnMapper.getAllData();
	}

	public int getDataCountByCondition(AccPsn entity) {
		return accPsnMapper.getDataCountByCondition(entity);
	}

	public List<AccPsn> getDataByCondition(AccPsn entity) {
		return accPsnMapper.getDataByCondition(entity);
	}

	public int getDataCountByLikeCondition(AccPsn entity) {
		return accPsnMapper.getDataCountByLikeCondition(entity);
	}

	public List<AccPsn> getDataByLikeCondition(AccPsn entity) {
		return accPsnMapper.getDataByLikeCondition(entity);
	}

	public List<AccPsn> getDataByIds(Long[] ids) {
		return accPsnMapper.getDataByIds(ids);
	}

	public void insertData(AccPsn entity) {
		insertData(entity, false);
	}

	/**
	 * 添加业主信息
	 * 设置账户状态为0
	 * 更新房屋信息InfoHouse 将state更改为1
	 * @param entity
	 * @param setIdValue
	 */
	public void insertData(AccPsn entity, boolean setIdValue) {
		if (setIdValue) {
			entity.setId(getInsertId());
		}
		
		InfoHouse infoHouse = infoHouseService.getDataById(entity.getHouseId());
		infoHouse.setState("1");
		infoHouseService.updateData(infoHouse);
		
		InfoBld infoBld = infoBldService.getDataById(infoHouse.getBldId());
		
		entity.setState("0");
		
		Double depScale = processService.depScale(infoHouse.getFirstPayMode(), infoBld.getBldType(), infoHouse.getHouseAttr(), infoHouse.getHouseUse(), infoHouse.getLiftFlag()); 
		
		entity.setFPayDue(infoHouse.getStruArea() * depScale / 100);
		entity.setQueryPass(MD5.MD5Encode("666666"));
		entity.setRoomName(infoHouse.getRoomName());
		entity.setHouseAddr(infoHouse.getResName() + infoHouse.getBldName() + infoHouse.getCellName() + infoHouse.getRoomName());
		entity.setFHousePrice(infoHouse.getHousePrice());
		entity.setFPayArea(infoHouse.getStruArea());
		
		accPsnMapper.insertData(entity);
	}

	public void updateData(AccPsn entity) {
		accPsnMapper.updateData(entity);
	}

	public void updateDataByParam(Map<String, Object> map) {
		if (map.size() == 0)
			return;

		accPsnMapper.updateDataByParam(map);
	}

	public void deleteDataByObject(AccPsn entity) {
		accPsnMapper.deleteDataByObject(entity);
	}

	public void deleteDataById(Long id) {
		accPsnMapper.deleteDataById(id);
	}

	public void deleteDataByIds(Long[] ids) {
		accPsnMapper.deleteDataByIds(ids);
	}

}
